var menuList = getMineOrderMenu(),
	commonMenu = getMineCommonMenu(),
	orderMenuList = getOrderMenuList(),
	userInfo = {},
	userDoms = {
		name: null,
		userId: null,
		avatar: null,
		messageUnread: null
	},
	orderMenu = null,
	userMenu = null;

$(() => {
	orderMenuList.shift();//是数组对象的一个内置方法，用于移除数组的第一个元素并返回该元素，同时数组的长度会减1
	initDoms();
	queryUserInfo();
	initMenu();
	queryOrderCounts();
	queryMenuCounts();
	queryMessageCount();
})

initDoms = () => {
	userDoms.name = document.getElementById('name');
	userDoms.userId = document.getElementById('user_id');
	userDoms.avatar = document.getElementById('avatar');
	userDoms.messageUnread = document.getElementById('message_count');
	document.getElementById('message').addEventListener('click', e => {
		navigateTo(`page/index/message.html`)
	})
	document.getElementById('settings').addEventListener('click', e => {
		navigateTo(`page/settings/index.html`)
	})
	document.getElementById('order_all').addEventListener('click', e => {
		navigateTo(`page/order/index.html?index=0`)
	})
	document.getElementById('user_info').addEventListener('click', e => {
		navigateTo(`page/user/personal.html`)
	})
}

queryUserInfo = () => {
	setTimeout(() => {
		getUserInfo({}, res => {
			userInfo = res;
			setUserInfo();
		})
	}, 1000)
}

setUserInfo = () => {
	const {
		name,
		id,
		avatar
	} = userInfo;
	userDoms.name.innerHTML = name;
	userDoms.userId.innerHTML = `吃货号：` + id;
	userDoms.avatar.src = avatar;
}

queryOrderCounts = () => {
	setTimeout(() => {
		let data = {
			'pay': 15,
			'send': 3,
			'receive': 1,
			'evaluate': 6
		}
		updateOrderCount(data);
	}, 1000)
}

updateOrderCount = e => {
	const updatedOrderMenu = orderMenuList.map(item => ({
		...item,
		value: e[item.key] ?? item.value
	}));
	orderMenu.update(updatedOrderMenu);
}

queryMenuCounts = () => {
	setTimeout(() => {
		let data = {
			'coupon': 31,
			'point': 1562,
			'collect': 15
		}
		updateMenuCount(data);
	}, 1000)
}

updateMenuCount = e => {
	menuList.forEach(item => {
		if (e.hasOwnProperty(item.key)) {
			item.value = e[item.key];
		}
	});
	userMenu.update(menuList);
}

queryMessageCount = () => {
	setTimeout(() => {
		let res = {
			code: 200,
			data: 123456
		}
		let count = res.data;
		if (parseInt(count) == 0) {
			userDoms.messageUnread.style.display = 'none';
		} else {
			userDoms.messageUnread.innerHTML = count > 99 ? '99+' : count;
		}
	}, 500)
}

initMenu = () => {
	orderMenu = new OrderMenu({
		data: orderMenuList,
		container: "#order_menu",
		onClickItem: (item) => {
			handleMenuItem(item)
		}
	})
	userMenu = new Menu({
		data: menuList,
		container: "#menu_user",
		onClickItem: (item) => {
			handleMenuItem(item)
		}
	})
	new Menu({
		data: commonMenu,
		container: "#menu_common",
		onClickItem: (item) => {
			handleMenuItem(item)
		}
	})
}

handleMenuItem = e => {
	const {
		url,
		num
	} = e;
	url ?
		url.includes('order') ? navigateTo(`${url}.html?index=${e.index}`) :
		navigateTo(`${url}.html`) :
		onCallphone(num)
}
